package com.it.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_1)
    Button mTv1;
    @BindView(R.id.tv_2)
    Button mTv2;

    @BindView(R.id.tv_3)
    Button tv3;

    OkHttpClient client = new OkHttpClient();
    String url =
            "http://210.75.207.51:7001/Meteorological/power_powerObserve.action?type=wind&areacode=&level=500kV";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


    }

    long t1 = 0;

    @OnClick({R.id.tv_1, R.id.tv_2, R.id.tv_3})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_1:
                /*new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            OkGo.get(url).tag("1").execute();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();*/
                t1 = System.currentTimeMillis();
                System.out.println("start1_________________");
                OkGo.<String>get(url).tag("1").cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE).execute(new StringCallback() {


                    @Override
                    public void onSuccess(Response<String> response) {
                        System.out.println((System.currentTimeMillis() - t1) + response.body());


                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        System.out.println("缓存:" + response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        System.out.println("errorororororoo");


                    }
                });


                break;
            case R.id.tv_2:

                Request request = new Request.Builder()
                        .url(url)
                        .build();
                System.out.println("start2_________________");
                client.newCall(request).enqueue(new Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, okhttp3.Response response) throws IOException {
                        System.out.println("end2_________________");

                    }
                });


                break;
            case R.id.tv_3:

                OkGo.<PointVal>get(url).tag("3").cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE).execute(new JsonCallback<PointVal>() {
                    @Override
                    public void onSuccess(Response<PointVal> response) {
                        System.out.println("得到的数据:"+response.body().time);
                    }

                    @Override
                    public void onError(Response<PointVal> response) {
                        super.onError(response);
                        System.out.println("得到的数据:errorororo");

                    }
                });


                break;
        }
    }
}
